Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Operators in Java

Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.

Table Of Content
  • Operators in Java
  • Types of Operators
  • Java Operator Precedence
  • Java Left Shift Operator
  • Java Right Shift Operator
  • Java Ternary Operator
  • Java AND Operator:Logical && and Bitwise &

Types of Operators

There are many types of operators in Java which are given below

  • Unary Operator
  • Arithmetic Operator
  • Shift Operator
  • Relational Operator
  • Bitwise Operator
  • Logical Operator
  • Ternary Operator
  • Assignment Operator





Operator Type Category Precedence
Unary postfix
prefix
expr++ expr--
++expr --expr +expr -expr
Arithmetic multiplication/divison
addition/subtraction
* / %
+ -
Shift shift <<>> >>>
Relational comparison
equality
<> <=>= instanceof
== !=
Bitwise bitwise AND
bitwise exclusive OR
bitwise inclusive OR
&
^
|
Logical logical AND
logical OR
&&
||
Ternary ternary ? :
Assignment assignment = += -= *= /= %= &= ^= |= <<=>>= >>>=

Java Unary Operator

The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.

  • Incrementing/decrementing a value by one
  • Negating an expression
  • Inverting the value of a boolean
 // Java Unary Operator Example : ++ and --
 import java.io.*;
 public class UnaryOperator{  
public static void main(String args[ ]){
int x =  20;  // In postfix value is first used  then increment by 1
System.out.println("X : "+ x++);  // 20, after this line value is increase to 21 will result in next line  
System.out.println("X : "+ x);  //  it will print 21   
System.out.println("X : "+ ++x);  //  The variable's value initially rises by 1, and then it is utilised in the expression   
System.out.println("X : "+ x--);  //  it will print 22   
System.out.println("X : "+ --x);  //  it will print 20   
 }
}




Output:

X : 20 
X : 21 
X : 22 
X : 22 
X : 20 
 // Java Unary Operator Example : ~ and !
 import java.io.*;
 public class UnaryOperator{  
public static void main(String args[ ]){
int x =  20; 
int y =  -20; 
boolean  a = true ; 
boolean  b = false ; 
System.out.println(~x);  // -21, minus of total positive value which starts from 0  
System.out.println(~y);  // 19, positive of total minus, positive starts from 0   
System.out.println(!a);  // false opposite of  ture boolean value     
System.out.println(!b);  // true opposite of false boolean value    
 }
}

Output:

-21 
19  
false 
true 
Java Arithmetic Operators

Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.

 import java.io.*;
 public class UnaryOperator{  
public static void main(String args[ ]){
int x =  20; 
int y =  10; 
System.out.println(x+y);  // 30  
System.out.println(x-y); // 10   
System.out.println(x*y); // 200  
System.out.println(x/y); // 2    
System.out.println(x%y); // 0    
 }
}



Output:

30 
10 
200 
2 
0 
Java Left Shift Operator

The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.

 import java.io.*;
 public class UnaryOperator{  
public static void main(String args[ ]){
System.out.println(2<<2);  // 2*2^2=2*4=8  
System.out.println(5<<3); // 5*2^3=5*8=40   
System.out.println(3<<5); // 3*2^5=3*32=96  
System.out.println(8<<3); // 8*2^2=8*4=32    
System.out.println(15<<4); // 15*2^4=15*16=240    
 }
}

Output:

8 
40 
96 
32 
240 
Java Right Shift Operator

The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.

 import java.io.*;
 public class UnaryOperator{  
public static void main(String args[ ]){
System.out.println(20>>2);  // 20/2^2=20/4=5    
System.out.println(40>>3); // 40/2^3=40/5=8   
System.out.println(20>>2); // 20/2^2=20/4=5  
System.out.println(32>>4); // 32/2^4=32*16=2    
System.out.println(20>>3); // 20/2^3=20/8=2    
 }
}



Output:

5 
8 
5 
2 
2 
Difference betwwen >> vs >>>

>> is arithmetic shift right, >>> is logical shift right.

 import java.io.*;
 public class UnaryOperator{  
public static void main(String args[ ]){
System.out.println(Integer.toBinaryString(121));  // prints 1111001    
System.out.println(Integer.toBinaryString(121 >> 1)); // prints 111100   
System.out.println(Integer.toBinaryString(121 >>> 1)); // prints 111100  
 }
}

Output:

1111001 
111100 
111100 
Java AND Operator: Logical && and Bitwise &

The logical && operator doesn't check the second condition if the first condition is false. It checks the second condition only if the first one is true.

The bitwise & operator always checks both conditions whether first condition is true or false.

 // Java AND Operator Example: Logical && and Bitwise &
 import java.io.*;
 public class LogicalOperator{  
public static void main(String args[ ]){
int x = 20; 
int y = 10; 
int z = 5; 
System.out.println(x<y && y>z);  // false && true =false    
System.out.println(z>y & y<x); // ture && false = false   
 }
}

Output:

false 
false 
Java AND Operator Example 2: Logical && and Bitwise &
 // Java AND Operator Example 2: Logical && and Bitwise &
 import java.io.*;
 public class LogicalOperator{  
public static void main(String args[ ]){
int x =  8; 
int y =  4; 
int z =  15; 
System.out.println(x<y && x++ <z);  //false && true = false      
System.out.println(x); // value of x is not increase as second condition is not checked   
System.out.println(x<y & x++ <z);  //false && true = false      
System.out.println(x); // value of x is  increase as second condition is  checked   
 }
}



Output:

false 
8 
false 
9 
Java OR Operator Example: Logical || and Bitwise |

The logical || operator doesn't check the second condition if the first condition is true. It checks the second condition only if the first one is false.

The bitwise | operator always checks both conditions whether first condition is true or false.

 // Java AND Operator Example: Logical || and Bitwise | 
 import java.io.*;
 public class LogicalOperator{  
public static void main(String args[ ]){
int x =  8; 
int y =  4; 
int z =  15; 
System.out.println(x>y || x<z);  //true || true = true     
System.out.println(x>y | x<z);  //true | true = true     
System.out.println(x>y | x++ <z);  //true | true = true        
System.out.println(x); // value of x is  increase as second condition is  checked   
 }
}

Output:

ture 
ture 
true 
9 



Java Ternary Operator

Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java programming. It is the only conditional operator which takes three operands.

 // Java AND Operator Example: Logical || and Bitwise | 
 import java.io.*;
 public class LogicalOperator{  
public static void main(String args[ ]){
int x =  8; 
int y =  4; 
int minimum =  (x<y)?x:y;   
System.out.println(minimum);       
 }
}

Output:

4 
Java Assignment Operator

Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.

 // Java Assignment Operator Example 
 import java.io.*;
 public class LogicalOperator{  
public static void main(String args[ ]){
int x =  8; 
int y =  4; 
x= x+4;   
y= y-4;  
System.out.println(x);  //8+4=12     
System.out.println(y);   // 4-4=0    
 }
}



Output:

12 
0 
Java Keywords Next »
«Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Naming Convention
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.